import numpy as np
import matplotlib.pyplot as plt




def inMixdata2float(org_array,new_array):
    for index in range(0,len((org_array))):
        item = org_array[index]
        if item != b"":
            item = item.decode('gb2312')
            item = float(item)
        else:
            item = None
        new_array[index]   =item


def defectsCop(data_array,threshold):
    for index in range(0,len((data_array))):
        item =data_array[index]
        if item>=float(threshold):
            item = None
        data_array[index]=item
def bise(dataArray):
    for index in range(0,len(dataArray)):
        if np.isnan(dataArray[index]):
            if index==0:
                dataArray[index] =  dataArray[index + 1]
            elif index==len(dataArray):
                dataArray[index] = dataArray[index - 1]
            else:
                dataArray[index]=0.5*(dataArray[index-1]+dataArray[index+1])

(humidity_str,gas_str,co_str)=np.loadtxt('ug_detect.csv',dtype=bytes,delimiter=',',skiprows=1,usecols=(2,3,4),unpack=True)

humidity = np.ndarray(len(humidity_str))
gas = np.ndarray(len(gas_str))
co = np.ndarray(len(co_str))
inMixdata2float(humidity_str,humidity)
defectsCop(humidity,200)
inMixdata2float(gas_str,gas)
defectsCop(gas,100)
inMixdata2float(co_str,co)
defectsCop(co,100)


bise(humidity)
bise(gas)
bise(co)
print("井下的显度是:",humidity)
print("井下的瓦斯气体浓度是:",gas)
print("井下的一氧化碳浓度是:",co)


print("保存处理后的显度数据文件")
np.savetxt('ug_humidity.csv',humidity,delimiter=',',fmt='%.2f')

print("保存处理后的瓦斯气体浓度数据文件")
np.savetxt('ug_gas.csv',gas,delimiter=',',fmt='%.2f')

print("保存处理后的一氧化碳浓度数据文件")
np.savetxt('ug_co.csv',co,delimiter=',',fmt='%.2f')

all_data =np.array([humidity,gas,co]).transpose()
np.savetxt("all_data_numpy.csv",all_data,header="显度,瓦斯气体浓度,一氧化碳浓度",delimiter=",",comments="",fmt='%2.f')